import { _decorator, BoxCollider2D, CircleCollider2D, Component, EventTouch, Input, instantiate, Node, PolygonCollider2D, Prefab, RigidBody2D, tween, UITransform, Vec2, Vec3 } from 'cc';
import { LayerAction } from './LayerAction';
import { ElementAction } from './ElementAction';
import { HoleAction } from './HoleAction';
import { PinAction } from './PinAction';

import { LevelAction } from './LevelAction';
import { ResourcePool } from './ResourcePool';
import { Tools } from './Tools';
import { ColorDate } from './ColorDate';
import { Global } from './Global';
const { ccclass, property } = _decorator;

@ccclass('LayerRootAction')
export class LayerRootAction extends Component {
    lvl_action:LevelAction
    update(deltaTime: number) {
    }

    public init_root(lvl: number) {
        Tools.clearFromParent(this.node, LevelAction);
        let level_prebab = ResourcePool.instance.get_prefab("lvl_" + lvl);
        let unit_arr:Prefab[] = [];
        if (!level_prebab) {
            console.log("没有这个关卡》〉》〉》", lvl);
            //return
            // console.log("重置一个等级1");
            // Global.restart_default(0);
            // lvl = Global.next_level();
            // level_prebab = ResourcePool.instance.get_prefab("lvl_" + lvl);
            
             level_prebab = ResourcePool.instance.get_prefab("lvl_x");
             unit_arr = this.get_lvl_unit(lvl);
        }
        let lvl_node = instantiate(level_prebab);
        if(unit_arr.length>0){
            console.log("生成关卡,unit num:",unit_arr.length);
            
            unit_arr.forEach(unit_prefab => {
                let unit_node = instantiate(unit_prefab);
                lvl_node.addChild(unit_node);
            });
        }
        
        this.lvl_action = lvl_node.getComponent(LevelAction)
        this.lvl_action.init_level();
        this.node.addChild(lvl_node);
    }

    private get_lvl_unit(lvl:number):Prefab[]{
        
        let unit_arr:Prefab[] = ResourcePool.instance.get_prefabs_for_unit();
        unit_arr.sort(() => {
            return 0.5 - Math.random();
        }); 
        let num = 1;
        if(lvl<15){
            num = 4;
        }else if (lvl<20){
            num = 5;
        }
        else if (lvl<25){
            num = 6;
        }else{
            num = 10;
        }

        let ret:Prefab[] = [];
        for(let i =0;i<num;i++){
            if(unit_arr.length>0){
                ret.push(unit_arr.shift())
            }
        }
        return ret;
    }

    random_pin() {
        let arr: PinAction[] = [];
        if(this.lvl_action) {
           arr =  this.lvl_action.get_pin_color();
        }

        let t = 0.05;
        let y = this.node.getPosition().y;
        tween(this.node)
            .to(t, { position: new Vec3(30, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(-30, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(30, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(-30, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(20, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(-20, y,0) }, Global.our_easing)
            .to(t, { position: new Vec3(20, y,0) }, Global.our_easing)
            .to(t, { position: new Vec3(-20, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(10, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(-10, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(2, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(-2, y,0) },Global.our_easing)
            .to(t, { position: new Vec3(0, y,0) },Global.our_easing)
            .delay(0.3)
            .call(() => {
                this.node.setPosition(new Vec3(0, y,0));

                let arr_color: ColorDate[] = [];
                arr.forEach(element => {
                    let c = element.pin_color;
                    arr_color.push(c.clone());
                });
                arr_color.sort(() => {
                    return 0.5 - Math.random();
                });
                if (arr.length == arr_color.length) {
                    for (let i = 0; i < arr.length; i++) {
                        let pin_color = arr[i].pin_color;
                        let c = arr_color[i];
                        pin_color.r = c.r;
                        pin_color.g = c.g;
                        pin_color.b = c.b;
                        pin_color.id = c.id;
                        arr[i].reset_img();
                    }
                }
            })
            .start();


    }

}

